package Array;

import leetcodeHis.ListNode;

import java.util.List;

public class Duplicate {
    public  static int removeDuplicates(int[] nums) {
        if(nums.length == 0) {
            return 0;
        }
        int index = 0;
        int cur = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if(i==0) {
                cur = nums[i];
            }else {
                if(nums[i] != cur) {
                    cur = nums[i];
                   index++;
                   nums[index] = nums[i];
                }
            }
        }
        return index+1;
    }


    //给定数组和值，原地
    public static int removeElement(int[] nums, int val) {
        int slow = 0 , fast = 0;
        while (fast < nums.length) {
            if(nums[fast] != val) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }

    public static void moveZeroes(int[] nums) {
        int i = removeElement(nums, 0);
        for (int j = i; j < nums.length; j++) {
            nums[j] = 0;
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {3,2,2,3};
        removeElement(nums1,3);
    }

    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null) {
            return null;
        }
        int cur = head.val;
        ListNode p = head;
        while (p != null) {
            ListNode next = p.next;
            if(next == null) {
                break;
            }
            if(next.val != cur) {
                cur = next.val;
                p= p.next;
            }else {
                p.next = next.next;
            }
        }
        return head;
    }
}
